package biz.inspeed.test.concurrent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

public class Mian0925 {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		final ReadThread t = new ReadThread();
		t.start();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				System.out.println("I'am sleeping...");
				try {
					TimeUnit.SECONDS.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("I'am waking up...");
				t.interrupt();
				System.out.println("try to interrupt the read thread!");
			}
		}).start();
	}

}

class ReadThread  extends Thread{
	
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	InputStream in = System.in;
	@Override
	public void run() {
		String line = null;
		int ch = 1;
		byte[] b = new byte[20	];
		try {
			while(!isInterrupted() && (ch = in.read(b)) != -1){
						 System.out.println("----------->" + ch);
					}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("---Interrupted-----");
	}
	
	@Override
	public void interrupt() {
		try {
			in.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		super.interrupt();
	}
}
